package com.ganzalang.gmall.designpatterns.strategy;

import lombok.extern.slf4j.Slf4j;

/**
 * @description: 使用模板方法
 * @author: ganzalang
 */
@Slf4j
public class StrategyPatternDemo {

    /**
     * 策略模式解决大量的if-else语句
     * @param args
     */
    public static void main(String[] args) {
        DiscountCalculateStrategy strategy = DiscountCalculateStrategyFactory.getByType(1);
        strategy.calculate();
    }

    public interface DiscountCalculateStrategy {
        void calculate();
    }

    public static class DiscountCalculateStrategyA implements DiscountCalculateStrategy {

        @Override
        public void calculate() {
            log.info("折扣A计算");
        }
    }

    public static class DiscountCalculateStrategyB implements DiscountCalculateStrategy {

        @Override
        public void calculate() {
            log.info("折扣B计算");
        }
    }

    public static class DiscountCalculateStrategyC implements DiscountCalculateStrategy {

        @Override
        public void calculate() {
            log.info("折扣C计算");
        }
    }

    public static class DiscountCalculateStrategyFactory {
        public static DiscountCalculateStrategy getByType(int discountType) {
            if (discountType == 1) {
                return new DiscountCalculateStrategyA();
            }else if (discountType == 2) {
                return new DiscountCalculateStrategyB();
            }else {
                return new DiscountCalculateStrategyC();
            }
        }
    }


}
